package cn.bugstack.domain.activity.service.discount;

import cn.bugstack.domain.activity.adapter.repository.IActivityRepository;
import cn.bugstack.domain.activity.model.valobj.DiscountTypeEnum;
import cn.bugstack.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import org.springframework.boot.autoconfigure.AutoConfigureOrder;

import javax.annotation.Resource;
import java.math.BigDecimal;

public abstract class AbstractDiscountCalculateService implements IDiscountCalculateService {

    @Resource
    private IActivityRepository repository;


    @Override
    public BigDecimal calculate(String userId, BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDisCount groupBuyDisCount) {

        if (DiscountTypeEnum.TAG.equals(groupBuyDisCount.getDiscountType())){
            boolean isCrowdRange =  filterTagId(userId,groupBuyDisCount.getTagId());
            if (!isCrowdRange) return originalPrice;
        }

        return doCalculate(originalPrice,groupBuyDisCount);
    }


    //抽象方法提供给子类使用
    protected abstract BigDecimal doCalculate(BigDecimal originalPrice, GroupBuyActivityDiscountVO.GroupBuyDisCount groupBuyDisCount);



    private boolean filterTagId(String userId, String tagId) {
        return repository.isTagCrowdRange(tagId,userId);
    }


}
